package com.example.mychat;

import com.example.mychat.redis.RedisUtils;
import com.example.mychat.webSocket.netty.NettyWebSocketStarter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.concurrent.Future;

/**
 * @Description
 * @Author susu
 * @Date 2024/9/5
 */
@Component
@Slf4j
public class InitRun implements ApplicationRunner {

    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Resource
    private DataSource dataSource;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private NettyWebSocketStarter nettyWebSocketStarter;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            dataSource.getConnection();
            redisUtils.get("test");
            Thread thread = new Thread(nettyWebSocketStarter);
            thread.start();
//            thread.join();
            log.info("服务启动成功");
        }catch (SQLException e){
            log.error("数据库配置错误，请检查数据库配置");
        }catch (RedisConnectionFailureException e){
            log.error("redis配置错误");
        }catch (Exception e){
            log.error("服务启动失败",e);
        }
        threadPoolTaskExecutor.submit(new Runnable() {
            @Override
            public void run() {

            }
        });

    }
}
